<?php

namespace App\Http\Controllers;

use App\Article;
use App\Column;
use App\Discuss;
use App\Http\Controllers\Controller;
use App\Msg;
use App\Question;
use App\Topic;
use App\User;
use Illuminate\Http\Request;

class HomeController extends Controller {
	/**
	 * Display a listing of the resource.
	 *
	 * @return \Illuminate\Http\Response
	 */
	public function index() {
		$articles = Article::where('status', '2')->orderBy('id', 'desc')->paginate(10);
		return view('home.index', ['articles' => $articles]);
	}

	/**
	 * 显示文章添加页面
	 */
	public function write() {
		$columns = Column::get();
		return view('home.write', ['columns' => $columns]);
	}

	/**
	 * @param Request $request
	 * @return mixed
	 * 执行文章添加操作
	 */
	public function doWrite(Request $request) {
		$this->validate($request, [
			'title' => 'required',
			'column' => 'required',
			'editorValue' => 'required',
		], [
			'title.required' => '文章标题不能为空',
			'column.required' => '请选择一个栏目!',
			'editorValue.required' => '内容不能为空',
		]);
		$art = new Article;
		$art->user_id = session('id');
		// $art->user_id = 1;
		$art->title = $request->title;
		$art->content = $request->editorValue;
		$art->topic_id = 1;
		$art->column_id = $request->column;
		$art->images = $request->input('images', '/uploads/default.jpg');
		$art->status = 1;
		$res = $art->save();
		if ($res) {
			$this->setLog(4, 2, $art->id);
		}
		return redirect('/');
	}

	/**
	 * 显示文章详情
	 */
	public function article($id) {

		$article = Article::findOrFail($id);
		$article->click++;
		$article->save();
		$article->discusses = Discuss::where('class_id', $id)->where('class', '1')->orderBy('created_at', 'desc')->get();
		// dd($article->discusses);
		return view('home.article', ['article' => $article]);
	}

	/**
	 * 显示某栏目下的文章
	 */
	public function column($id) {
		$articles = Article::where('column_id', $id)->where('status', '2')->get();
		// 获取所有文章
		foreach ($articles as $value) {
			$tmp[] = $value;
		}
		// dd($tmp);
		// dd($articles);
		// 如果该话题下又文章, 则显示文章列表, 否则, 404
		if (!empty($tmp[0])) {
			return view('home.list', ['articles' => $articles]);
		} else {
			return abort('404');
		}
	}

	/**
	 * 随机显示文章列表
	 */
	public function explode(Request $request) {
		$articles = Article::orderBy('discuss', 'desc')->where('status', '2')->paginate(10);

		// foreach ($articles as $k => $v) {
		// 	dd($v->user);
		// }
		return view('home.explode', ['articles' => $articles]);
	}

	/**
	 * 私信列表
	 */
	public function message(Request $request) {
		$msg = Msg::where('send_id', session('id'));
		return view('home.msg');
	}

	/**
	 * 话题列表
	 */
	public function topic(Request $request) {
		$columns = Column::get();
		$topics = Column::findOrFail(Column::first()->id)->topic;
		return view('home.topic', ['columns' => $columns, 'topics' => $topics]);
	}

	// 某话题下的所有文章
	public function getArticle($id) {

		// 获取关联表中的数据
		$topic = Topic::findOrFail($id)->topicArticle;
		// dd($topic);
		// 获得该话题下所有文章的ID
		$topicArticle = [];
		foreach ($topic as $k => $v) {
			$topicArticle[] = $v->article_id;
		}
		// dd($topicArticle);

		// 获取所有文章
		foreach ($topicArticle as $value) {
			$tmp = Article::where('id', $value)->where('status', '2')->first();
			if ($tmp) {
				$articles[] = $tmp;
			}
		}
		// dd($articles);
		// 如果该话题下又文章, 则显示文章列表, 否则, 404
		if (!empty($articles[0])) {
			return view('home.list', ['articles' => $articles]);
		} else {
			return abort('404');
		}
	}

	// 搜索结果
	public function search(Request $request) {
		$article = Article::where('title', 'like', '%' . $request->keywords . '%')->where('status', 2)->paginate(10);
		// dd($article->last());
		$user = User::where('name', 'like', '%' . $request->keywords . '%')->paginate(10);
		$topic = Topic::where('title', 'like', '%' . $request->keywords . '%')->paginate(10);
		$question = Question::where('content', 'like', '%' . $request->keywords . '%')->paginate(10);
		// dd($topic);
		return view('home.search', ['article' => $article, 'user' => $user, 'topic' => $topic, 'question' => $question]);
	}

	// 问题列表
	public function question() {
		$questions = Question::orderBy('id', 'desc')->paginate(10);
		foreach ($questions as $key => $value) {
			// dd($value->user);
		}

		return view('home.question', ['questions' => $questions]);
	}

	// 问题详情页
	public function questionShow($id) {
		$question = Question::findOrFail($id);
		$question->discusses = Discuss::where('class_id', $id)->where('class', '2')->orderBy('created_at', 'desc')->paginate(5);

		return view('home.questionShow', ['question' => $question]);
	}

	// 密码修改成功
	public function success(Request $request) {
		if ($request->password != $request->repassword) {
			return view('remind.success', ['info' => '修改失败!']);
		}
		$user = User::find(session('id'));
		if ($request->oldpassword) {
			if (!Hash::check($request->oldpassword, $user->password)) {
				return view('remind.success', ['info' => '修改失败!']);
			}
		}
		$user->password = Hash::make($request->password);
		if ($user->save()) {
			return view('remind.success', ['info' => '修改成功!']);
		} else {
			return view('remind.success', ['info' => '修改失败!']);
		}
	}
}
